﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntitiesTM;
using Business;

namespace TimeWork
{
    public partial class Dashboard : System.Web.UI.Page
    {
        public int time_second;
        public int time_min;
        public static string companyid;

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadContent();
            
        }

        private void LoadContent()
        {
            if (Session["email"] != null)
            {
                companyid = Session["companyid"].ToString();

                Panel overlay = (Panel)Master.FindControl("overlay");
                overlay.Visible = false;
                // Lay thoi gian trong cookies
                if (GetCookie("Time") != null)
                {
                    string cookie = GetCookie("Time").Value;
                    pnlStart.Visible = false;
                    pnlStop.Visible = true;
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "StartTimer", "ShowTime('" + cookie + "')", true);
                }
                else
                {
                    pnlStart.Visible = true;
                    pnlStop.Visible = false;
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "StopTimer", "StopTime()", true);
                }

                if (!IsPostBack)
                {
                    lblDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    lblTime.Text = DateTime.Now.ToString("dd/MM/yyyy") + " " + DateTime.Now.ToLongTimeString();
                    BindDdlProject();
                    BindDdlActivity(Int32.Parse(ddlProject.SelectedValue));
                    showGreeting();
                    loadContent();
                    BindDdlTypeTime();
                    
                }
            }
        }
        private void showGreeting()
        {
            if (Session["email"] != null)
            {
                string email = Session["email"].ToString();
                EnStaff staff = BuStaff.GetStaffByEmailOpt(email, companyid);
                lblUser.Text = staff.staffName;
            }
        }

        private void loadContent()
        {
            if (Session["email"] != null)
            {
                string email = Session["email"].ToString();
                DateTime date = GetFirstDayOfWeek(DateTime.Now);
                ltReportedTime.Text = BuMain.selReportedTime(date, email, companyid);
                ltReportedTimeActivity.Text = BuMain.selReportedTimeActivity(DateTime.Now, email, companyid);
                ////////////////////////////////////
                //////////////////////////////////
                /////Can bo sung sau
                //////////////////////////////////
                ltMyProject.Text = BuMain.selMyProject(email, companyid);
                ltWeekend.Text = BuMain.selWeekend(DateTime.Now, email, companyid);
            }
        }

        public DateTime GetFirstDayOfWeek(DateTime dt)
        {
            int day = (int)dt.DayOfWeek;
            if (day == 0)
            {
                return dt.AddDays(-6);
            }
            return dt.AddDays(1 - day);
        }

        protected void btnStart_Click(object sender, EventArgs e)
        {
            // Lay thoi gian CLient va luu vao cookies

            SetCookie("Time", hdTime.Value, 30);
            pnlStart.Visible = false;
            pnlStop.Visible = true;

            Response.Redirect("~/Dashboard.aspx");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            DelCookies("Time");
            pnlStop.Visible = false;
            pnlStart.Visible = true;
            Response.Redirect("~/Dashboard.aspx");
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            pnlOverlay.Visible = true;
            // Lam mo backgroud
            Panel overlay = (Panel)Master.FindControl("overlay");
            overlay.Visible = true;
        }
        

        public void SetCookie(string name, string value, int expireDays)
        {
            HttpCookie cookie = new HttpCookie(name);
            cookie.Value = value;
            cookie.Expires = DateTime.Now.AddDays(expireDays);
            Response.Cookies.Add(cookie);
        }

        public HttpCookie GetCookie(string name)
        {
            return Request.Cookies[name];
        }

        public void DelCookies(string name)
        {
            HttpCookie cookie = new HttpCookie(name, "");
            cookie.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(cookie);
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            pnlOverlay.Visible = false;
            Panel overlay = (Panel)Master.FindControl("overlay");
            overlay.Visible = false;
        }

        private void BindDdlTypeTime()
        {
            ddlTypeTime.DataSource = BuProject.selAllTypeTime(companyid);
            ddlTypeTime.DataTextField = "TypeTimeName";
            ddlTypeTime.DataValueField = "TypeTimeID";
            ddlTypeTime.DataBind();
        }

        protected void btnRegisterTime_Click(object sender, EventArgs e)
        {
            Panel overlay = (Panel)Master.FindControl("overlay");
            overlay.Visible = false;

            // Dang ky thoi gian
            EnSA sa = new EnSA();
            lblError.Text = String.Empty;

            string activityID = ddlActivity.SelectedValue;
            int hour = Int32.Parse(ddlHour.SelectedValue);
            int minute = Int32.Parse(ddlMinute.SelectedValue);
            bool isValidActivity = true;
            bool isValidTime = true;
            if (activityID.Equals(""))
            {

                isValidActivity = false;
                lblError.Text += "* Sản phẩm này chưa đuợc phân công công việc. Hãy liên hệ với quản trị để bổ sung công việc.<br>";
            }

            if (hour == 0 && minute == 0)
            {
                isValidTime = false;
                lblError.Text += "* Thời gian đăng ký phải lớn hơn 0.<br>";
            }

            if (isValidActivity && isValidTime)
            {
                sa.saActivityID = Int32.Parse(activityID);
                sa.saTimeReportHour = hour;
                sa.saTimeReportMinute = minute;
                sa.saDate = DateTime.Now;
                sa.email = Session["email"].ToString();
                sa.saDetails = txtCommentTime.Text;
                sa.saTypeTimeID = Int32.Parse(ddlTypeTime.SelectedValue);

                BuTimeDay.insRegisterDay(sa, companyid);

                DelCookies("Time");
                if (rbStop.Checked)
                {
                    pnlStop.Visible = false;
                    pnlStart.Visible = true;
                }
                else
                {
                    SetCookie("Time", hdTime.Value, 30);
                    pnlStart.Visible = false;
                    pnlStop.Visible = true;
                }
                pnlOverlay.Visible = false;
                Response.Redirect("~/Dashboard.aspx");
            }

        }

        private void BindDdlProject()
        {
            //ddlProject.DataSource = BuProject.selProjectRegister2(Session["email"].ToString());
            //ddlProject.DataTextField = "ProjectName";
            //ddlProject.DataValueField = "ProjectID";
            //ddlProject.DataBind();

            Dictionary<string, string> data = BuProject.selProjectByParentIDTreeReport(0, companyid);
            ListItem item;
            item = new ListItem("Tất cả sản phẩm", "0");
            ddlProject.Items.Add(item);

            foreach (KeyValuePair<string, string> pair in data)
            {
                item = new ListItem(pair.Value, pair.Key);
                ddlProject.Items.Add(item);
            }
        }

        private void BindDdlActivity(int projectID)
        {
            EnProject project = new EnProject();
            project.projectID = projectID;
            try
            {
                ddlActivity.DataSource = BuProject.selActivityByProjectExceptID(project, companyid);
                ddlActivity.DataTextField = "ActivityName";
                ddlActivity.DataValueField = "ActivityID";
                ddlActivity.DataBind();
            }
            catch { }
        }

        protected void ddlProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            string itemSelected = ddlProject.SelectedValue;
            BindDdlActivity(Int32.Parse(itemSelected)); 
        }


    }
}